package com.example.chatchatbackend.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chatchatbackend.entity.dto.Sensitivewords;
import com.example.chatchatbackend.entity.vo.response.MessageVO;
import com.example.chatchatbackend.entity.vo.response.SensitivewordsVO;
import com.example.chatchatbackend.mapper.SensitivewordsMapper;
import com.example.chatchatbackend.service.MessageService;
import com.example.chatchatbackend.service.SensitivewordsService;
import com.example.chatchatbackend.utils.TimeUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 丁真
 * @since 2024-10-28 05:03:47
 */
@Slf4j
@Service
public class SensitivewordsServiceImpl extends ServiceImpl<SensitivewordsMapper, Sensitivewords> implements SensitivewordsService {

    private static final LocalDateTime currentTime  = TimeUtil.Time_Now.getCurrentTime();

    @Autowired
    private SensitivewordsMapper sensitivewordsMapper;
    @Resource
    private MessageService messageService;

    @Override
    public List<SensitivewordsVO> getAllSensitivewords() {
        List<Sensitivewords> sensitivewordsList = this.query().list();
        List<SensitivewordsVO> sensitivewordsVOList = new ArrayList<>();
        for(Sensitivewords sensitivewords : sensitivewordsList){
            sensitivewordsVOList.add(new SensitivewordsVO(sensitivewords.getId(),sensitivewords.getWord(),sensitivewords.getContent(),sensitivewords.getCreatedTime(),sensitivewords.getUpdateTime()));
        }
        return sensitivewordsVOList;
    }

    @Override
    public SensitivewordsVO insertSensitivewords(SensitivewordsVO sensitivewordsVO) {
        Sensitivewords comment = new Sensitivewords(null,sensitivewordsVO.getWord(),sensitivewordsVO.getContent(),this.currentTime,this.currentTime);
        if(this.save(comment))
            return sensitivewordsVO;
        return null;
    }

    @Override
    public List<SensitivewordsVO> insertSensitivewordsList(List<SensitivewordsVO> sensitivewordsVOList) {

        List<Sensitivewords> sensitivewordsList = new ArrayList<>();

        for (SensitivewordsVO sensitivewordsVO : sensitivewordsVOList) {
            sensitivewordsList.add(new Sensitivewords(null,sensitivewordsVO.getWord(),sensitivewordsVO.getContent(),this.currentTime,this.currentTime));
        }
        sensitivewordsMapper.insertList(sensitivewordsList);
        return sensitivewordsVOList;
    }

    @Override
    public IPage<Sensitivewords> pageThree(Integer pageNum, Integer pageSize) {
        Page<Sensitivewords> page = new Page<>(pageNum, pageSize);
        IPage<Sensitivewords> sensitivewordsIPage = sensitivewordsMapper.selectPage(page, null);
        return sensitivewordsIPage;
    }

    @Override
    public SensitivewordsVO updateSensitivewords(SensitivewordsVO sensitivewordsVO) {
        Sensitivewords sensitivewords = new Sensitivewords();
        BeanUtils.copyProperties(sensitivewordsVO, sensitivewords);
        sensitivewords.setUpdateTime(this.currentTime);
        sensitivewordsMapper.updateSensitivewords(sensitivewords);
        return sensitivewordsVO;
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        sensitivewordsMapper.deleteBatchIds(ids);
    }
    
    @Override
    public List<SensitivewordsVO> findByIds(List<Integer> ids) {
        List<Sensitivewords> sensitivewordsList = sensitivewordsMapper.selectBatchIds(ids);
        List<SensitivewordsVO> sensitivewordsVOList = new ArrayList<>();

        for (Sensitivewords sensitivewords : sensitivewordsList) {
            SensitivewordsVO sensitivewordsVO = new SensitivewordsVO();
            BeanUtils.copyProperties(sensitivewords, sensitivewordsVO);
            sensitivewordsVOList.add(sensitivewordsVO);  // 添加到结果列表
        }

        return sensitivewordsVOList;
    }

    public SensitivewordsVO checkMessagesAndRespond(List<Integer> ids)  {
        List<MessageVO> messages = messageService.findByIds(ids);
        if (messages.isEmpty()) {
            return null;
        }
        String content = messages.get(0).getMessage();
        List<SensitivewordsVO> keywords = this.getAllSensitivewords();


        for (SensitivewordsVO keyword : keywords) {
            if (content.contains(keyword.getWord())) {
                // 触发自动回复
                log.info("识别到{}", keyword);
                return keyword; // 返回找到的第一个敏感词
            }
        }

        return null; // 如果没有找到敏感词，返回 null
    }
}
